<template>
    <div class="chat-view">
        <div class="title">{{customer.uname}}</div>
        <ul class="content" ref="content">
            <li v-for="(item, index) in customer.message" :key="index">
                <div class="message" >
                    <img v-if="$store.state.businessUser._id != item.uId" :src="customer.avatar" alt="">
                    <img v-else src="http://localhost:3000/public/img/customerService/customer-service.png" alt="">
                    <p :class="{customer: $store.state.businessUser._id != customer.uId}">{{item.msg}}</p>
                </div>
            </li>
        </ul>
        <div class="send">
            <textarea name="" id="" cols="30" rows="10" style="width: 100%;resize:none" v-model="textarea"></textarea>
            <el-button type="primary" @click="send">发送</el-button>
        </div>
    </div>
</template>

<script>
    export default {
        name: "ChatView",
        props: {
            customer: {
                type: Object,
                default() {
                    return {}
                }
            }
        },
        data() {
            return {
                textarea: ''
            }
        },
        watch: {
            'customer.message': function () {
                setTimeout(()=> {
                    this.$refs.content.scrollTop = this.$refs.content.scrollHeight
                }, 100)
            }
        },
        methods: {
            send() {
                this.$emit('reply', {
                    text: this.textarea,
                    toId: this.customer.uId
                })
                this.textarea = ''
            }
        }
    }
</script>

<style scoped>
    .chat-view {
        display: flex;
        flex-flow: column ;
        justify-content: space-between;
        height: 100%;
    }

    .title {
        height: 70px;
        line-height: 70px;
        background-color: #666666;
        text-align: center;
    }
    .content {
        flex: 1;
        overflow: auto;
        list-style: none;
    }
    .send {
        width: 100%;
        border: 1px solid #000000;
        overflow: hidden;
        display: flex;
    }
    .message {
        display: flex;
        align-items: center;
    }
    .message img {
        width: 40px;
        height: 40px;
        margin-right: 1em;
    }
    .message p {
        word-wrap:break-word;
        max-width: 40em;
        min-height: 2.5em;
        line-height: 2.5em;
        padding: 0 1em;
        border-radius: 8px;
    }
    .customer {
        background-color: #ffffff;
        border: 1px solid black;
    }
</style>